﻿//
// Lightbox Gallery Module for DotNetNuke
// Project Contributors - Will Strohl (http://www.WillStrohl.com), Armand Datema (http://www.schwingsoft.com)
//
//Copyright (c) 2009-2013, Will Strohl
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other 
//materials provided with the distribution.
//
//Neither the name of Will Strohl, Armand Datema, Lightbox Gallery, nor the names of its contributors may be 
//used to endorse or promote products derived from this software without specific prior 
//written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
//TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
//CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
//DAMAGE.
//

//INSTANT C# NOTE: Formerly VB project-level imports:

using System.Globalization;
using DotNetNuke.Common.Utilities;
using System;
using System.Collections;
using System.Data;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.Search;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace WillStrohl.Modules.Lightbox
{

	public sealed class LightboxController : IPortable, ISearchable
	{

#region  Constants 

		public const string SCRIPT_TAG_FORMAT = "<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>";
		public const string STYLESHEET_TAG_FORMAT = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
		public const string IMAGE_THUMBNAIL_PATTERN = "-thumb$0";
		public const string IMAGE_FILE_PATTERN = "\\.(png|gif|jpg)$";
		public const string FOLDER_NAME_MATCH_PATTERN = "^[\\/]";
		public const string FOLDER_NAME_REPLACE_PATTERN = "[\\/](.*)";
		public const string FOLDER_NAME_REPLACEMENT_PATTERN = "$1";
		public const string THUMBNAIL_MATCH_PATTERN = "-thumb\\.(png|gif|jpg)$";
        public const string BACK_SLASH = "\\";
        public const string FORWARD_SLASH = "/";

		private const string STANDARD_FOLDER_PROVIDER = "StandardFolderProvider";

#endregion

#region  Data Access 

		public int AddLightbox(LightboxInfo objLightbox)
		{
			return DataProvider.Instance().AddLightbox(objLightbox.ModuleId, objLightbox.GalleryName, objLightbox.GalleryDescription, objLightbox.GalleryFolder, objLightbox.DisplayOrder, objLightbox.HideTitleDescription, objLightbox.LastUpdatedBy, objLightbox.GroupId);
		}
		public void UpdateLightbox(LightboxInfo objLightbox)
		{
			DataProvider.Instance().UpdateLightbox(objLightbox.LightboxId, objLightbox.ModuleId, objLightbox.GalleryName, objLightbox.GalleryDescription, objLightbox.GalleryFolder, objLightbox.DisplayOrder, objLightbox.HideTitleDescription, objLightbox.LastUpdatedBy, objLightbox.GroupId);
		}
		public void DeleteLightbox(int LightboxId)
		{
			DataProvider.Instance().DeleteLightbox(LightboxId);
		}
		public LightboxInfo GetLightbox(int LightboxId)
		{

			var objInj = new LightboxInfo();
			objInj.Fill(DataProvider.Instance().GetLightbox(LightboxId));
			return objInj;

		}
		public LightboxInfoCollection GetLightboxes(int ModuleId)
		{

            var collInj = new LightboxInfoCollection();
			collInj.Fill(DataProvider.Instance().GetLightboxes(ModuleId));
			return collInj;

		}
		public List<int> GetLightboxIds(int ModuleId)
		{

            var arrId = new List<int>();
			IDataReader rdr = DataProvider.Instance().GetLightboxIds(ModuleId);

			while (rdr.Read())
			{
				arrId.Add(int.Parse(rdr[LightboxInfoMembers.LightboxIdField].ToString(), System.Globalization.NumberStyles.Integer));
			}

			return arrId;

		}
		/// <summary>
		/// GetLightboxCount - this method returns a total count of the albums per the given ModuleId.
		/// </summary>
		/// <param name="ModuleId">Integer - the ID number for the module</param>
		/// <returns>Integer - the total number of albums for the module</returns>
		/// <remarks></remarks>
		/// <history>
		/// 20101013 - wstrohl - Added error handling for 0-albums use case
		/// </history>
		public int GetLightboxCount(int ModuleId)
		{

			int intReturn = 0;

			try
			{
				intReturn = DataProvider.Instance().GetLightboxCount(ModuleId);
			}
			catch (NullReferenceException ex)
			{
				// do nothing - this happened because there were no albums
			}

			return intReturn;

		}
		public bool DoesDisplayOrderNeedUpdate()
		{
			return DataProvider.Instance().DoesDisplayOrderNeedUpdate();
		}
		public void UpdateDisplayOrder()
		{
			DataProvider.Instance().UpdateDisplayOrder();
		}


		public int AddSetting(SettingInfo Settings)
		{
			return DataProvider.Instance().AddSetting(Settings.LightboxId, Settings.Padding, Settings.Margin, Settings.Opacity, Settings.Modal, Settings.Cyclic, Settings.OverlayShow, Settings.OverlayOpacity, Settings.OverlayColor, Settings.TitleShow, Settings.TitlePosition, Settings.Transition, Settings.Speed, Settings.ChangeSpeed, Settings.ShowCloseButton, Settings.ShowNavArrows, Settings.EnableEscapeButton, Settings.OnStart, Settings.OnCancel, Settings.OnComplete, Settings.OnCleanup, Settings.OnClosed);
		}
		public void UpdateSetting(SettingInfo Settings)
		{
			DataProvider.Instance().UpdateSetting(Settings.SettingId, Settings.LightboxId, Settings.Padding, Settings.Margin, Settings.Opacity, Settings.Modal, Settings.Cyclic, Settings.OverlayShow, Settings.OverlayOpacity, Settings.OverlayColor, Settings.TitleShow, Settings.TitlePosition, Settings.Transition, Settings.Speed, Settings.ChangeSpeed, Settings.ShowCloseButton, Settings.ShowNavArrows, Settings.EnableEscapeButton, Settings.OnStart, Settings.OnCancel, Settings.OnComplete, Settings.OnCleanup, Settings.OnClosed);
		}
		public void DeleteSetting(int SettingId)
		{
			DataProvider.Instance().DeleteSetting(SettingId);
		}
		public SettingInfo GetSettings(int LightboxId)
		{
            var objSetting = new SettingInfo();
			objSetting.Fill(DataProvider.Instance().GetSettings(LightboxId));

			if (objSetting != null && objSetting.SettingId > 0)
			{
				return objSetting;
			}
			else
			{
				return null;
			}
		}


		public ImageInfoCollection GetImages(int LightboxId)
		{
            var collImage = new ImageInfoCollection();
			collImage.Fill(DataProvider.Instance().GetImages(LightboxId));
			return collImage;
		}
		public ImageInfo GetImageById(int ImageId)
		{
            var objImage = new ImageInfo();
			objImage.Fill(DataProvider.Instance().GetImageById(ImageId));

			if (objImage != null && objImage.ImageId > 0)
			{
				return objImage;
			}
			else
			{
				return null;
			}
		}
		public ImageInfo GetImageByFileName(int LightboxId, string FileName)
		{
            var objImage = new ImageInfo();
			objImage.Fill(DataProvider.Instance().GetImageByFileName(LightboxId, FileName));

			if (objImage != null && objImage.ImageId > 0)
			{
				return objImage;
			}
			else
			{
				return null;
			}
		}
		public int AddImage(ImageInfo Image)
		{
			return DataProvider.Instance().AddImage(Image.LightboxId, Image.FileName, Image.Title, Image.Description, Image.DisplayOrder, Image.LastUpdatedBy);
		}
		public int AddImage(int LightboxId, string FileName, string Title, string Description, int DisplayOrder, int LastUpdatedBy)
		{
			return DataProvider.Instance().AddImage(LightboxId, FileName, Title, Description, DisplayOrder, LastUpdatedBy);
		}
		public void UpdateImage(ImageInfo Image)
		{
			DataProvider.Instance().UpdateImage(Image.ImageId, Image.LightboxId, Image.FileName, Image.Title, Image.Description, Image.DisplayOrder, Image.LastUpdatedBy);
		}
		public void UpdateImage(int ImageId, int LightboxId, string FileName, string Title, string Description, int DisplayOrder, int LastUpdatedBy)
		{
			DataProvider.Instance().UpdateImage(ImageId, LightboxId, FileName, Title, Description, DisplayOrder, LastUpdatedBy);
		}
		public void DeleteImageByFileName(int LightboxId, string FileName)
		{
			DataProvider.Instance().DeleteImageByFileName(LightboxId, FileName);
		}
		public void DeleteImageById(int ImageId)
		{
			DataProvider.Instance().DeleteImageById(ImageId);
		}
		public int GetImageCount(int LightboxId)
		{
			return DataProvider.Instance().GetImageCount(LightboxId);
		}

#endregion

#region  Path Helpers 

		/// <summary>
		/// ReformatUrlForWeb - this method performs the steps necessary to translate a local path to a virtual path
		/// </summary>
		/// <param name="Path">Path to parse</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string ReformatUrlForWeb(string Path)
		{

            return Path.Replace(BACK_SLASH, FORWARD_SLASH);

		}

		/// <summary>
		/// ReformatPathForLocal - this method performs the steps necessary to translate a virtual path to a local path
		/// </summary>
		/// <param name="Path">Path to parse</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string ReformatPathForLocal(string Path)
		{

            return Path.Replace(FORWARD_SLASH, BACK_SLASH);

		}

#endregion

#region  Image Helpers 

		public IFileInfo GetImageFromProvider(int PortalId, string FolderName, string FileName)
		{

			IFolderInfo oFolder = FolderManager.Instance.GetFolder(PortalId, FolderName);
			IFileInfo oImage = FileManager.Instance.GetFile(oFolder, FileName);

			return oImage;

		}

		public IFileInfo GetImageFromProvider(int PortalId, IFolderInfo Folder, string FileName)
		{

			IFileInfo oImage = FileManager.Instance.GetFile(Folder, FileName);
			return oImage;

		}

		/// <summary>
		/// GetThumbnailImageName - Takes the original file name and returns the thumbnail equivalent
		/// </summary>
		/// <param name="File">Full or partial path, or just the file name that you want to translate</param>
		/// <param name="Settings">Current portal settings with HttpContext</param>
		/// <param name="ReplaceSlash">True, if you want to switch the black slashes to forward slashes</param>
		/// <returns>String - the translated file name</returns>
		/// <remarks></remarks>
		public static string GetThumbnailImageName(string File, PortalSettings Settings, bool ReplaceSlash)
		{
			if (ReplaceSlash)
			{
				return ReformatUrlForWeb(Regex.Replace(File, IMAGE_FILE_PATTERN, IMAGE_THUMBNAIL_PATTERN, RegexOptions.IgnoreCase).Replace(Settings.HomeDirectoryMapPath, string.Empty));
			}
			else
			{
				return Regex.Replace(File, IMAGE_FILE_PATTERN, IMAGE_THUMBNAIL_PATTERN, RegexOptions.IgnoreCase);
			}
		}

		/// <summary>
		/// VerifiedFilesList - returns a string array of file names that match the modules supported file types
		/// </summary>
		/// <param name="Files">An array object of the file names</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static ArrayList VerifiedFileList(IEnumerable<IFileInfo> Files)
		{
			return VerifiedFileList(Files, false);
		}

		/// <summary>
		/// VerifiedFilesList - returns a string array of file names that match the modules supported file types
		/// </summary>
		/// <param name="Files">An array object of the file names</param>
		/// <param name="ThumbnailsOnly">If true, only the thumbnails will be returned</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static ArrayList VerifiedFileList(IEnumerable<IFileInfo> Files, bool ThumbnailsOnly)
		{
			var arrFiles = new ArrayList();

			if (! ThumbnailsOnly)
			{

				foreach(IFileInfo oFile in Files)
				{
					// do not include non-image files in the list, also exclude thumbnails that have already been generated
					if (Regex.IsMatch(oFile.FileName, IMAGE_FILE_PATTERN, RegexOptions.IgnoreCase) && ! (Regex.IsMatch(oFile.FileName, THUMBNAIL_MATCH_PATTERN, RegexOptions.IgnoreCase)))
					{
						arrFiles.Add(oFile);
					}
				}

			}
			else
			{

				foreach(IFileInfo oFile in Files)
				{
					// do not include non-image files in the list, and only includes thumbnails
					if (Regex.IsMatch(oFile.FileName, THUMBNAIL_MATCH_PATTERN, RegexOptions.IgnoreCase))
					{
						arrFiles.Add(oFile);
					}
				}

			}

			return arrFiles;
		}

		/// <summary>
		/// GetImageFileUrl - this method returns the valid URL for any file, regardless to folder or folder provider in use
		/// </summary>
		/// <param name="Image">Fully loaded IFileInfo object</param>
		/// <returns></returns>
		/// <remarks>
		/// WARNING!!! This method can return exceptions. They should be caught and processed in the UI though.
		/// </remarks>
		public string GetImageFileUrl(IFileInfo Image)
		{

			//*******************************************************'
			// WARNING!!!
			// This method can return exceptions. They should be 
			// caught and processed in the UI though.
			//*******************************************************'
		    try
		    {
		        if (Image != null)
		        {
		            FolderMappingInfo mapFolder = FolderMappingController.Instance.GetFolderMapping(Image.FolderMappingID);
		            return FolderProvider.Instance(mapFolder.FolderProviderType).GetFileUrl(Image);
		        }
		        else
		        {
		            return string.Empty;
		        }
		    }
		    catch (Exception ex)
		    {
                // write some information to the event log to make sure we can troubleshoot this later

                EventLogController ctlLog = new EventLogController();
		        PortalSettings oSettings = PortalSettings.Current;
		        UserInfo oUser = UserController.GetCurrentUserInfo();
		        LogProperties props = null;
		        int UserId = -1;

                // generate the details for troubleshooting
		        if (oUser != null && oUser.UserID > Null.NullInteger)
		        {
		            UserId = oUser.UserID;
		            props = new LogProperties
		                {
		                    new LogDetailInfo("User", oUser.Username),
                            new LogDetailInfo("UserID", oUser.UserID.ToString()),
		                    new LogDetailInfo("FileId", Image.FileId.ToString()),
                            new LogDetailInfo("FileName", Image.FileName),
                            new LogDetailInfo("Folder", Image.Folder),
                            new LogDetailInfo("FolderId", Image.FolderId.ToString()),
                            new LogDetailInfo("FolderMappingId", Image.FolderMappingID.ToString())
		                };
		        }
		        else
		        {
                    props = new LogProperties
		                {
		                    new LogDetailInfo("FileId", Image.FileId.ToString()),
                            new LogDetailInfo("FileName", Image.FileName),
                            new LogDetailInfo("Folder", Image.Folder),
                            new LogDetailInfo("FolderId", Image.FolderId.ToString()),
                            new LogDetailInfo("FolderMappingId", Image.FolderMappingID.ToString())
		                };
		        }

                // write to the event log
		        ctlLog.AddLog(props, oSettings, UserId, EventLogController.EventLogType.ADMIN_ALERT.ToString(), true);

                // send the error back to the UI
		        throw ex;
		    }

		}

		/// <summary>
		/// IsLocalFolder - this method allows you to determine if the file is in a local or remote (cloud) folder
		/// </summary>
		/// <param name="FileObject">Fully loaded IFileInfo object</param>
		/// <returns></returns>
		/// <remarks>
		/// </remarks>
		public bool IsLocalFolder(IFileInfo FileObject)
		{

			//*******************************************************'
			// WARNING!!!
			// This method can return exceptions. They should be 
			// caught and processed in the UI though.
			//*******************************************************'
			FolderMappingInfo mapFolder = FolderMappingController.Instance.GetFolderMapping(FileObject.FolderMappingID);
			return string.Equals(mapFolder.FolderProviderType, STANDARD_FOLDER_PROVIDER);

		}

#endregion

#region  IPortable Implementation 

		public string ExportModule(int ModuleID)
		{
			var sb = new StringBuilder();
			var collInj = new LightboxInfoCollection();
			collInj = GetLightboxes(ModuleID);
			var objSetting = new SettingInfo();

			sb.Append("<WillStrohl><Lightboxes>");
			foreach(LightboxInfo obj in collInj)
			{
				sb.Append("<Lightbox>");

				sb.AppendFormat("<LightboxId>{0}</LightboxId>", obj.LightboxId);
				sb.AppendFormat("<ModuleId>{0}</ModuleId>", obj.ModuleId);
				sb.AppendFormat("<GalleryName>{0}</GalleryName>", XmlUtils.XMLEncode(obj.GalleryName));
				sb.AppendFormat("<GalleryDescription>{0}</GalleryDescription>", XmlUtils.XMLEncode(obj.GalleryDescription));
				sb.AppendFormat("<GalleryFolder>{0}</GalleryFolder>", XmlUtils.XMLEncode(obj.GalleryFolder));
				sb.AppendFormat("<DisplayOrder>{0}</DisplayOrder>", obj.DisplayOrder);
				// 20101013 - Add HideTitleDescription
				sb.AppendFormat("<HideTitleDescription>{0}</HideTitleDescription>", obj.HideTitleDescription);
				// 20120214 - Added auditing
				sb.AppendFormat("<LastUpdatedBy>{0}</LastUpdatedBy>", obj.LastUpdatedBy);
				sb.AppendFormat("<LastUpdatedDate>{0}</LastUpdatedDate>", obj.LastUpdatedDate);
                // 20121023 - Added GroupId for social integration
			    sb.AppendFormat("<GroupId>{0}</GroupId>", obj.GroupId);

				objSetting = GetSettings(obj.LightboxId);
				sb.Append("<Settings>");
				sb.AppendFormat("<ChangeSpeed>{0}</ChangeSpeed>", objSetting.ChangeSpeed);
				sb.AppendFormat("<Cyclic>{0}</Cyclic>", objSetting.Cyclic);
				sb.AppendFormat("<EnableEscapeButton>{0}</EnableEscapeButton>", objSetting.EnableEscapeButton);
				sb.AppendFormat("<Margin>{0}</Margin>", objSetting.Margin);
				sb.AppendFormat("<Modal>{0}</Modal>", objSetting.Modal);
				sb.AppendFormat("<OnCancel>{0}</OnCancel>", XmlUtils.XMLEncode(objSetting.OnCancel));
				sb.AppendFormat("<OnCleanup>{0}</OnCleanup>", XmlUtils.XMLEncode(objSetting.OnCleanup));
				sb.AppendFormat("<OnClosed>{0}</OnClosed>", XmlUtils.XMLEncode(objSetting.OnClosed));
				sb.AppendFormat("<OnComplete>{0}</OnComplete>", XmlUtils.XMLEncode(objSetting.OnComplete));
				sb.AppendFormat("<OnStart>{0}</OnStart>", XmlUtils.XMLEncode(objSetting.OnStart));
				sb.AppendFormat("<Opacity>{0}</Opacity>", objSetting.Opacity);
				sb.AppendFormat("<OverlayColor>{0}</OverlayColor>", XmlUtils.XMLEncode(objSetting.OverlayColor));
				sb.AppendFormat("<OverlayOpacity>{0}</OverlayOpacity>", objSetting.OverlayOpacity);
				sb.AppendFormat("<OverlayShow>{0}</OverlayShow>", objSetting.OverlayShow);
				sb.AppendFormat("<Padding>{0}</Padding>", objSetting.Padding);
				sb.AppendFormat("<ShowCloseButton>{0}</ShowCloseButton>", objSetting.ShowCloseButton);
				sb.AppendFormat("<ShowNavArrows>{0}</ShowNavArrows>", objSetting.ShowNavArrows);
				sb.AppendFormat("<Speed>{0}</Speed>", objSetting.Speed);
				sb.AppendFormat("<TitlePosition>{0}</TitlePosition>", XmlUtils.XMLEncode(objSetting.TitlePosition));
				sb.AppendFormat("<TitleShow>{0}</TitleShow>", objSetting.TitleShow);
				sb.AppendFormat("<Transition>{0}</Transition>", XmlUtils.XMLEncode(objSetting.Transition));
				sb.Append("</Settings>");

				ImageInfoCollection collImage = GetImages(obj.LightboxId);
				sb.Append("<Images>");
				foreach(ImageInfo img in collImage)
				{
					sb.Append("<Image>");
					sb.AppendFormat("<ImageId>{0}</ImageId>", img.ImageId);
					sb.AppendFormat("<LightboxId>{0}</LightboxId>", img.LightboxId);
					sb.AppendFormat("<FileName>{0}</FileName>", XmlUtils.XMLEncode(img.FileName));
					sb.AppendFormat("<Title>{0}</Title>", XmlUtils.XMLEncode(img.Title));
					sb.AppendFormat("<Description>{0}</Description>", XmlUtils.XMLEncode(img.Description));
					sb.AppendFormat("<DisplayOrder>{0}</DisplayOrder>", img.DisplayOrder);
					// 20120214 - Added auditing
					sb.AppendFormat("<LastUpdatedBy>{0}</LastUpdatedBy>", img.LastUpdatedBy);
					sb.AppendFormat("<LastUpdatedDate>{0}</LastUpdatedDate>", img.LastUpdatedDate);
					sb.Append("</Image>");
				}
				sb.Append("</Images>");

				sb.Append("</Lightbox>");
			}
			sb.Append("</Lightboxes>");

			// later on, will probably need to add module settings here

			sb.Append("</WillStrohl>");

			return sb.ToString();
		}

		public void ImportModule(int ModuleID, string Content, string Version, int UserID)
		{

			try
			{
				XDocument xContents = XDocument.Parse(Content);

				foreach(XElement xLightbox in xContents.Descendants("Lightbox"))
				{

					var objLightbox = new LightboxInfo();
					objLightbox.ModuleId = ModuleID;
					objLightbox.GalleryName = xLightbox.Element("GalleryName").Value;
					objLightbox.GalleryDescription = xLightbox.Element("GalleryDescription").Value;
					objLightbox.GalleryFolder = xLightbox.Element("GalleryFolder").Value;
					objLightbox.DisplayOrder = int.Parse(xLightbox.Element("DisplayOrder").Value, NumberStyles.Integer);

				    if (xLightbox.Element("HideTitleDescription") != null)
					{
						objLightbox.HideTitleDescription = bool.Parse(xLightbox.Element("HideTitleDescription").Value);
					}
					else
					{
						objLightbox.HideTitleDescription = false;
					}

					objLightbox.LastUpdatedBy = UserID;
					objLightbox.LastUpdatedDate = DateTime.Now;

                    try
                    {
                        objLightbox.GroupId = int.Parse(xLightbox.Element("GroupId").Value, NumberStyles.Integer);
                    }
                    catch
                    {
                        // do nothing
                        // the import might be made from an older version of the module
                    }

					objLightbox.LightboxId = AddLightbox(objLightbox);

					foreach(XElement xSetting in xLightbox.Descendants("Settings"))
					{

						var objSetting = new SettingInfo();
						objSetting.ChangeSpeed = int.Parse(xSetting.Element("ChangeSpeed").Value, System.Globalization.NumberStyles.Integer);
						objSetting.Cyclic = bool.Parse(xSetting.Element("Cyclic").Value);
						objSetting.EnableEscapeButton = bool.Parse(xSetting.Element("EnableEscapeButton").Value);
						objSetting.LightboxId = objLightbox.LightboxId;
						objSetting.Margin = int.Parse(xSetting.Element("Margin").Value, System.Globalization.NumberStyles.Integer);
						objSetting.Modal = bool.Parse(xSetting.Element("Modal").Value);
						objSetting.OnCancel = xSetting.Element("OnCancel").Value;
						objSetting.OnCleanup = xSetting.Element("OnCleanup").Value;
						objSetting.OnClosed = xSetting.Element("OnClosed").Value;
						objSetting.OnComplete = xSetting.Element("OnComplete").Value;
						objSetting.OnStart = xSetting.Element("OnStart").Value;
						objSetting.Opacity = bool.Parse(xSetting.Element("Opacity").Value);
						objSetting.OverlayColor = xSetting.Element("OverlayColor").Value;
						objSetting.OverlayOpacity = decimal.Parse(xSetting.Element("OverlayOpacity").Value, System.Globalization.NumberStyles.Float);
						objSetting.OverlayShow = bool.Parse(xSetting.Element("OverlayShow").Value);
						objSetting.Padding = int.Parse(xSetting.Element("Padding").Value, System.Globalization.NumberStyles.Integer);
						objSetting.ShowCloseButton = bool.Parse(xSetting.Element("ShowCloseButton").Value);
						objSetting.ShowNavArrows = bool.Parse(xSetting.Element("ShowNavArrows").Value);
						objSetting.Speed = int.Parse(xSetting.Element("Speed").Value, System.Globalization.NumberStyles.Integer);
						objSetting.TitlePosition = xSetting.Element("TitlePosition").Value;
						objSetting.TitleShow = bool.Parse(xSetting.Element("TitleShow").Value);
						objSetting.Transition = xSetting.Element("Transition").Value;

						AddSetting(objSetting);
					}

					foreach(XElement xImage in xLightbox.Descendants("Image"))
					{

                        var objImage = new ImageInfo();
						objImage.LightboxId = objLightbox.LightboxId;
						objImage.FileName = xImage.Element("FileName").Value;
						objImage.Title = xImage.Element("Title").Value;
						objImage.Description = xImage.Element("Description").Value;
						objImage.DisplayOrder = int.Parse(xImage.Element("DisplayOrder").Value, System.Globalization.NumberStyles.Integer);

						objImage.LastUpdatedBy = UserID;
						objImage.LastUpdatedDate = DateTime.Now;

						AddImage(objImage);

					}

				}

			}
			catch (Exception ex)
			{
				Exceptions.LogException(ex);
			}

		}

#endregion

#region  ISearchable Implementation 

		public SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
		{

			var searchItems = new SearchItemInfoCollection();
			LightboxInfoCollection collLightbox = GetLightboxes(ModInfo.ModuleID);

			if (collLightbox != null)
			{

				// iterate through each album
				foreach(LightboxInfo oLightbox in collLightbox)
				{

					var siLightbox = new SearchItemInfo();

					siLightbox.Author = oLightbox.LastUpdatedBy;
					siLightbox.Content = oLightbox.GalleryName;
					siLightbox.Description = oLightbox.GalleryDescription;
					siLightbox.ModuleId = oLightbox.ModuleId;
					siLightbox.PubDate = oLightbox.LastUpdatedDate;
					siLightbox.SearchItemId = oLightbox.LightboxId;
					siLightbox.SearchKey = oLightbox.LightboxId.ToString();
					siLightbox.Title = oLightbox.GalleryName;
					searchItems.Add(siLightbox);

					// iterate through each image title & description
					ImageInfoCollection collImage = GetImages(oLightbox.LightboxId);

					if (collImage != null && collImage.Count > 0)
					{

						foreach(ImageInfo oImage in collImage)
						{

                            var siImage = new SearchItemInfo();

							siImage.Author = oImage.LastUpdatedBy;
							siImage.Content = oImage.Title;
							siImage.Description = oImage.Description;
							siImage.ModuleId = oLightbox.ModuleId;
							siImage.PubDate = oImage.LastUpdatedDate;
							siImage.SearchItemId = oImage.ImageId;
							siImage.SearchKey = oImage.LightboxId.ToString();
							siImage.Title = oImage.Title;
							searchItems.Add(siImage);

						}

					}

				}

			}

			return searchItems;

		}

#endregion

		}

}